#include <iostream>
#include <sys/iofunc.h>
#include <sys/dispatch.h>
#include <errno.h>
#include "packet.h"
#include "database.h"

using namespace std;


#define ATTACH_POINT "database"

int main(void){
	DataBase baza;
	name_attach_t *server;
	int rcvid;
	packet_t msg;
	packet_t reply;
	 
	if ((server = name_attach(NULL, ATTACH_POINT, NAME_FLAG_ATTACH_GLOBAL)) == NULL) {
		cerr << "Blad przypisania nazwy serwera.";
		return -1;
	}

	/* Do your MsgReceive's here now with the chid */
	while (1) {
		rcvid = MsgReceive(server->chid, &msg, sizeof(msg), NULL);

   	       if (rcvid == -1) {/* Error condition, exit */
    	       break;
       		}
		

#if 1
	       if (rcvid == 0) {/* Pulse received */
    	       switch (msg.hdr.code) {
        		   case _PULSE_CODE_DISCONNECT:
						   /*
			                * A client disconnected all its connections (called
			                * name_close() for each name_open() of our name) or
			                * terminated
			                */
               				ConnectDetach(msg.hdr.scoid);
               				break;
           			case _PULSE_CODE_UNBLOCK:
                 			/*
			                * REPLY blocked client wants to unblock (was hit by
			                * a signal or timed out).  It's up to you if you
			                * reply now or later.
			                */
               				break;
           			default:
                 			/*
			                * A pulse sent by one of your processes or a
			                * _PULSE_CODE_COIDDEATH or _PULSE_CODE_THREADDEATH
			                * from the kernel?
			                */
           		
					continue;
				}
			}

       /* name_open() sends a connect message, must EOK this */
       if (msg.hdr.type == _IO_CONNECT ) {
           MsgReply( rcvid, EOK, NULL, 0 );
           continue;
       }

       /* Some other QNX IO message was received; reject it */
       if (msg.hdr.type > _IO_BASE && msg.hdr.type <= _IO_MAX ) {
           MsgError( rcvid, -1 );
           continue;
       }

 #endif
	cout<<"Otrzymalem wiadomosc who: "<<msg.who<<" typ: "<<msg.type<<endl;
	
       /* A message (presumable ours) received, handle */
       
       switch(msg.who)
       {
    	    case AIRPORT:
		{
			switch(msg.type)
			{
			    case PACKET_AIRPORT_ADD:
			    {
				baza.AddAirPort(msg.lotnisko); //zakladam, ze lotnisko nie wysle 
			    		    	//wiecej jak jednej nformacji o lotniskach		
				reply.who = DATABASE;
				reply.type = COMMAND_OK;
				MsgReply(rcvid, 0, &reply, sizeof(reply));
			    }
			    break;
			    
			
			}
		}
	    break;
		
	    case AIRPLANE:
		{
			switch(msg.type)
			{
			    case PACKET_AIRPORT_COUNT:
					reply.who = DATABASE;
					reply.type = PACKET_AIRPORT_COUNT;
					reply.samolot.state = (baza.GetAirPortsList()).size();
					MsgReply(rcvid, 0, &reply, sizeof(reply));
				break;				
			case PACKET_AIRPORTS_GET:
						reply.who = DATABASE;
						reply.type = PACKET_AIRPORTS_GET;
						reply.lotnisko = (baza.GetAirPortsList())[msg.samolot.state];
						MsgReply(rcvid, 0, &reply, sizeof(reply));
						cout<<"Wyslalem lotnisko dla samolotu: "<<reply.lotnisko.airportName<<endl;
			

				break;
			case PACKET_AIRPLANE_TYPE_GET:
				reply.who = DATABASE;
				reply.type = PACKET_AIRPLANE_TYPE_GET;
				reply.samolot_typ =  baza.GetRandomPlane();
				MsgReply(rcvid, 0, &reply, sizeof(reply));
				break;
			}
		}
	}//end of switch(msg.who)
	
	} //end od while
	
	name_detach(server, 0);

return 0;
}
